﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.UsersAndSecurity.Presenters;
using MyCommerce.Presentation.UsersAndSecurity.IViews;
using Innova.Microfinanzas.BusinessEntities;
using Innova.Microfinanzas.BusinessEntities.SystemEnums;
using Innova.FoundationFramework.DataTypes;

namespace MyCommerce.Windows.UsersAndSecurity.Views
{
    public partial class RoleFillView : BaseForm, IRoleFillView
    {
        private RoleFillPresenter pPresenter;

        public RoleFillView()
        {
            InitializeComponent();
            pPresenter = new RoleFillPresenter(this);
        }

        #region IRoleFillView Members

        public long IdItemEdit { get; set; }

        public OptionSelect OptionViewSelect { get; set; }

        public Role Get()
        {
            return Role.CreateRole(0, fieldRoleName.Text.Trim(), fieldIsActive.Checked);
        }

        public void Set(Role objUserToLoad)
        {
            IdItemEdit = objUserToLoad.IdRole;
            fieldRoleName.Text = objUserToLoad.RoleName;
            fieldIsActive.Checked = objUserToLoad.IsActive;
        }

        public void CloseAndSetResult(bool result)
        {
            if (result)
                Context.ShowInformationMessageBox("La información se guardó correctamente.", "Creación/Edición de usuarios.");
            Result = result;
            Close();
        }

        public event GenericEventHandler<Role> OnLoadView;

        public event EventHandler OnSave;

        public event EventHandler OnCancel;

        #endregion

        private bool ValidateView()
        {
            bool vResult = true;
            StringBuilder message = new StringBuilder();

            if (string.IsNullOrEmpty(fieldRoleName.Text.Trim()))
            {
                vResult = false;
                message.AppendLine("Debe ingresar el nombre del rol.");
            }

            if (!vResult)
            {
                Context.ShowErrorMessageBox(message.ToString(), "Validación de datos.");
            }

            return vResult;
        }

        private void RoleFillView_Load(object sender, EventArgs e)
        {
            Role objUser = null;
            OptionViewSelect = OptionSelect.Create;
            if (Parameter != null)
            {
                objUser = (Role)Parameter;
                OptionViewSelect = OptionSelect.Edit;
            }
            if (OnLoadView != null)
                OnLoadView(this, new GenericEventArgs<Role>(objUser));
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (OnCancel != null)
                OnCancel(this, new EventArgs());
        }

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateView())
                {
                    if (OnSave != null)
                        OnSave(this, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }
    }
}
